/*
 * Created on 2006-4-5
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.sitech.portal.system.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.sitech.ibnms.core.IPage;
import com.sitech.ibnms.core.JGridBaseAction;
import com.sitech.portal.system.domain.TblSYS_COMPANY;
import com.sitech.portal.system.domain.TblSYS_DEPT;
import com.sitech.portal.system.domain.TblSYS_LOGINMSG;
import com.sitech.portal.system.form.DepartDTO;
import com.sitech.portal.system.form.OrganForm;
import com.sitech.portal.system.service.ISysCompanyService;
import com.sitech.portal.system.service.ISysDeptService;
import com.sitech.portal.system.service.ISysLogInmsgService;

/**
 * @author Administrator
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class OrganAction extends JGridBaseAction {
	
	private ISysCompanyService sysCompanyService;
	private ISysDeptService sysDeptService;
	private ISysLogInmsgService sysLogInmsgService;
	private List companyList;
	private DepartDTO[] dto;
	private OrganForm organForm;
	private String flag;
	
	public ISysCompanyService getSysCompanyService() {
		return sysCompanyService;
	}

	public void setSysCompanyService(ISysCompanyService sysCompanyService) {
		this.sysCompanyService = sysCompanyService;
	}

	public ISysDeptService getSysDeptService() {
		return sysDeptService;
	}

	public void setSysDeptService(ISysDeptService sysDeptService) {
		this.sysDeptService = sysDeptService;
	}

	public ISysLogInmsgService getSysLogInmsgService() {
		return sysLogInmsgService;
	}

	public void setSysLogInmsgService(ISysLogInmsgService sysLogInmsgService) {
		this.sysLogInmsgService = sysLogInmsgService;
	}

	public List getCompanyList() {
		return companyList;
	}

	public void setCompanyList(List companyList) {
		this.companyList = companyList;
	}

	public DepartDTO[] getDto() {
		return dto;
	}

	public void setDto(DepartDTO[] dto) {
		this.dto = dto;
	}

	public OrganForm getOrganForm() {
		return organForm;
	}

	public void setOrganForm(OrganForm organForm) {
		this.organForm = organForm;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}
	
	public String loadOrganManage(){
		return SUCCESS;
	}

	/**
	 * 机构列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String listCompanyTree() {
		// liuwei add 20091118
		TblSYS_LOGINMSG user = this.getCurrentUser();
		TblSYS_COMPANY comp = new TblSYS_COMPANY();
		comp.setCOMP_ID((String) user.getCOMP_ID());

		// List list = dao.findByList(null); liuwei 注释 20091118
		// liuwei add 20091118
		companyList = sysCompanyService.findByList(null);
		return SUCCESS;//
	}

	/**
	 * 机构列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String loadCompanyTree() {
		// liuwei add 20091118
		TblSYS_LOGINMSG user = this.getCurrentUser();
		TblSYS_COMPANY comp = new TblSYS_COMPANY();
		comp.setCOMP_ID((String) user.getCOMP_ID());

		// List list = dao.findByList(null); liuwei 注释 20091118
		// liuwei add 20091118
		companyList = sysCompanyService.findByList(comp);

		return SUCCESS;//
	}

	/**
	 * 点击节点得到子树
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String loadManageTree() {
		String nodeID = this.getRequest().getParameter("nodeID");// 其父节点ID
		String rootID = this.getRequest().getParameter("rootID");// 要传入的索引
		String manage_unit_id = this.getRequest().getParameter("manage_unit_id");
		if (manage_unit_id != null && !"ALL".equals(manage_unit_id)) {

			manage_unit_id = "'" + manage_unit_id.replaceAll(",", "','") + "'";
		}
		if ((rootID == null) || (nodeID == null)) {
			nodeID = "1";
			rootID = "";
		}
		List list = null;
		if (rootID.length() <= 2) {
			TblSYS_LOGINMSG user = this.getCurrentUser();
			String DeptID = ((String) user.getDEPT_ID()).substring(0, 3);
			list = sysDeptService.findRootDepartByCompIdAndManageUnitID(rootID, "");

		} else {
			list = sysDeptService.findBySuperDeptId(rootID);
		}
		if (list != null && list.size() > 0) {
			dto = new DepartDTO[list.size()];
			for (int i = 0; i < list.size(); i++) {

				TblSYS_DEPT obj = (TblSYS_DEPT) list.get(i);
				dto[i] = new DepartDTO();
				dto[i].setDepartId(obj.getDEPT_ID());
				dto[i].setDepartName(obj.getDEPT_NAME());
				dto[i].setMANAGE_UNIT_ID(obj.getMANAGE_UNIT_ID());
				dto[i].setCount(sysDeptService.findCountBySuperDeptId(dto[i].getDepartId()));

			}
		}
		return SUCCESS;//
	}

	/**
	 * 点击节点得到子树
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String loadOrganTree() {

		String nodeID = this.getRequest().getParameter("nodeID");// 其父节点ID
		String rootID = this.getRequest().getParameter("rootID");// 要传入的索引
		if ((rootID == null) || (nodeID == null)) {
			nodeID = "1";
			rootID = "";
		}

		List list = null;

		if (rootID.length() <= 2) {
			// list =
			// dao.findRootDepartByCompIdAndManageUnitID(rootID,manage_unit_id);
			// liuwei 注释 20091118 list =
			// dao.findRootDepartByCompIdAndManageUnitID(rootID,"");
			// liuwei add 20091118
			TblSYS_LOGINMSG user = this.getCurrentUser();
			String DeptID = ((String) user.getDEPT_ID()).substring(0, 3);
			if ("100".equals(DeptID)) {
				list = sysDeptService.findRootDepartByCompIdAndManageUnitID(rootID, "");
			} else {
				list = sysDeptService.findByDeptId(DeptID);
			}
		} else {
			list = sysDeptService.findBySuperDeptId(rootID);
		}

		if (list != null && list.size() > 0) {
			dto = new DepartDTO[list.size()];
			for (int i = 0; i < list.size(); i++) {

				TblSYS_DEPT obj = (TblSYS_DEPT) list.get(i);
				dto[i] = new DepartDTO();
				dto[i].setDepartId(obj.getDEPT_ID());
				dto[i].setDepartName(obj.getDEPT_NAME());
				dto[i].setCount(sysDeptService.findCountBySuperDeptId(dto[i].getDepartId()));

			}
		}
		return SUCCESS;//
	}

	/**
	 * 公司编辑页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String editOrgan() {
		String orgId = this.getRequest().getParameter("DEPT_ID");
		organForm.setISMP_JSP_COMMAND(this.getRequest().getParameter("ISMP_JSP_COMMAND"));

		if (organForm.getISMP_JSP_COMMAND() != null
				&& organForm.getISMP_JSP_COMMAND().equals("add")) {
			String pk = sysCompanyService.generateKey();
			if (pk != null) {
				pk = String.valueOf(Integer.parseInt(pk) + 1);
			}
			organForm.setMID_ID(pk);
			return SUCCESS;
		}
		TblSYS_COMPANY c = null;
		if (orgId != null) {
			c = new TblSYS_COMPANY();
			c.setCOMP_ID(orgId);
			TblSYS_COMPANY obj = sysCompanyService.findCompanyObjByPk(c);
			organForm.setCompCode(obj.getCOMP_CODE());
			organForm.setCompDesc(obj.getCOMP_DESC());
			organForm.setCompName(obj.getCOMP_NAME());
			organForm.setCompType(obj.getCOMP_TYPE());
			organForm.setMID_ID(obj.getCOMP_ID());
			organForm.setMANAGE_UNIT_ID(obj.getMANAGE_UNIT_ID());
		} else {
			organForm.setMID_ID(orgId);
		}

		return SUCCESS;
	}

	/**
	 * 公司保存页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String saveOrgan() {
		flag = "true";
		try{
			String orgId = this.getRequest().getParameter("MID_ID");
			TblSYS_COMPANY c = new TblSYS_COMPANY();
			c.setCOMP_ID(orgId);
			c.setCOMP_CODE(organForm.getCompCode());
			c.setCOMP_DESC(organForm.getCompDesc());
			c.setCOMP_NAME(organForm.getCompName());
			c.setCOMP_TYPE(organForm.getCompType());
			c.setMANAGE_UNIT_ID(organForm.getMANAGE_UNIT_ID());
			TblSYS_COMPANY obj = sysCompanyService.findCompanyObjByPk(c);
			if (obj != null) {
				sysCompanyService.update(c);
			} else {
				sysCompanyService.add(c);
			}
		}catch(Exception e){
			flag = e.getMessage();
		}
		return SUCCESS;
	}

	/**
	 * 删除公司
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String delOrgan() {

		String COMP_ID = this.getRequest().getParameter("COMP_ID");

		TblSYS_COMPANY c = new TblSYS_COMPANY();
		c.setCOMP_ID(COMP_ID);
		sysCompanyService.delete(c);
		return SUCCESS;
	}
}
